package learningThread.threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * newCachedThreadPool 根据需要创建新线程的线程池
 *
 * 参数核心线程池corePoolSize = 0, maximumPoolSize = Integer.MAX_VALUE，
 * 这也就是说当任务被提交到newCachedThread线程池时，
 * 将会直接把任务放到SynchronousQueue任务队列中，maximumPool从任务队列中获取任务
 *
 */
public class TestNewCachedThreadExcutorPool {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ThreadPoolExecutor executorService1 = (ThreadPoolExecutor) executorService;
        for (int i = 0; i < 30; i++) {
            executorService.submit(() -> {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("当前线程核心线程数:" + executorService1.getCorePoolSize()
                        + "最大线程数:" + executorService1.getMaximumPoolSize()
                        + "线程存活时间:" + executorService1.getKeepAliveTime(TimeUnit.MILLISECONDS)
                        + "已完成的线程数:" + executorService1.getCompletedTaskCount()
                        + "存活的线程数为:" + executorService1.getActiveCount());
                System.out.println("当前线程池数量:" + executorService1.getPoolSize());
            });
        }
    }
}
